/*
 * Copyright 2008-2010 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public License Version 3
 * The license can be read in its entirety in the LICENSE.txt file accompanying this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of Libra.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see: http://www.gnu.org/licenses
 */

package net.cachapa.libra;

import java.util.LinkedList;

import net.cachapa.libra.About.AboutAdapter.AboutItem;

import android.app.ListActivity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class About extends ListActivity {
	private AboutAdapter aboutAdapter; 
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Custom title
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.about);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.abouttitle);
		
		TextView versionTextView = (TextView) findViewById(R.id.version);
		try {
			versionTextView.setText(getString(R.string.version) + " " +
					getPackageManager().getPackageInfo(getPackageName(), 0).versionName);
		} catch (NameNotFoundException e) {
			// No version? No problem!
		}
		
		aboutAdapter = new AboutAdapter(this);
		
		generateDevelopmentItems();
		generateTranslationItems();
		generateAcknowledgementItems();
		generateMoreInfoItems();
		
		setListAdapter(aboutAdapter);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		AboutItem item = aboutAdapter.getAboutItem(position);
		final Intent actionIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.getAction())); 
		try {
			startActivity(actionIntent);
		} catch (ActivityNotFoundException e) {
			Toast.makeText(this, R.string.error_email, Toast.LENGTH_LONG).show();
		}
	}
	
	private void generateDevelopmentItems() {
		aboutAdapter.addSeparator(R.string.development);
		aboutAdapter.addItem("GPLv3", "This is open source software", "http://www.gnu.org/copyleft/gpl.html", R.drawable.gplv3);
		aboutAdapter.addItem("Daniel Cachapa", "cachapa@gmail.com", "mailto:cachapa@gmail.com", R.drawable.cachapa);
	}
	
	private void generateTranslationItems() {
		aboutAdapter.addSeparator(R.string.translation);
		aboutAdapter.addItem("Anders Hansson", getString(R.string.swedish), null, R.drawable.sweden);
		aboutAdapter.addItem("Christian Hoelgaard", getString(R.string.danish), null, R.drawable.denmark);
		aboutAdapter.addItem("Yngve Landro", getString(R.string.norwegian_nynorsk), null, R.drawable.norway);
		aboutAdapter.addItem("Steinar van der Meer", getString(R.string.norwegian_bokmal), null, R.drawable.norway);
		aboutAdapter.addItem("Daniele Ottorogo", getString(R.string.italian), null, R.drawable.italy);
		aboutAdapter.addItem("David Sanz", getString(R.string.spanish), null, R.drawable.spain);
		aboutAdapter.addItem("Noel Val de Casas", getString(R.string.french), null, R.drawable.france);
		aboutAdapter.addItem("Ramona Müller", getString(R.string.german), null, R.drawable.germany);
		aboutAdapter.addItem("Ricardo Pinho", getString(R.string.portuguese), null, R.drawable.portugal);
		aboutAdapter.addItem("Вадим Бродский", getString(R.string.russian), null, R.drawable.russia);
		aboutAdapter.addItem("Karol Oslowski", getString(R.string.polish), null, R.drawable.poland);
		aboutAdapter.addItem("Robson Rosa", getString(R.string.brazilian_portuguese), null, R.drawable.brazil);
		aboutAdapter.addItem("Rolphin Huang", getString(R.string.chinese), null, R.drawable.china);
	}
	
	private void generateAcknowledgementItems() {
		aboutAdapter.addSeparator(R.string.acknowledgements);
		aboutAdapter.addItem("Ted Miller", getString(R.string.contributed_help_file), null, android.R.drawable.ic_menu_compass);
		aboutAdapter.addItem("The Hacker's Diet", "http://www.fourmilab.ch/hackdiet", "http://www.fourmilab.ch/hackdiet/www/hackdiet.html", R.drawable.hackersdiet);
		aboutAdapter.addItem("DailyBurn", "http://www.dailyburn.com", "http://www.dailyburn.com", R.drawable.dailyburn);
		aboutAdapter.addItem("Signpost Project", "http://code.google.com/p/oauth-signpost", "http://code.google.com/p/oauth-signpost", R.drawable.signpost);
		aboutAdapter.addItem("Android Icons", "http://www.androidicons.com", "http://www.androidicons.com", R.drawable.androidicons);
	}
	
	private void generateMoreInfoItems() {
		aboutAdapter.addSeparator(R.string.more_info);
		aboutAdapter.addItem(getString(R.string.project_webpage), "http://code.google.com/p/libra-android", "http://code.google.com/p/libra-android", R.drawable.icon);
	}



	public class AboutAdapter extends BaseAdapter implements ListAdapter {
		private Context context;
		private LinkedList<AboutItem> items;
		
		public AboutAdapter(Context context) {
			this.context = context;
			items = new LinkedList<AboutItem>();
		}
		
		public int getCount() {
			return items.size();
		}

		public void addItem(String title, String description, String action, int icon) {
			items.add(new AboutItem(title, description, action, icon, false));
		}
		
		public void addSeparator(int title) {
			items.add(new AboutItem(getString(title), null, null, 0, true));
		}
		
		public Object getItem(int position) {
			return position;
		}
		
		public AboutItem getAboutItem(int position) {
			return items.get(position);
		}
		
		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			AboutItem item = items.get(position);
			if (item.isSeparator()) {
				View aboutView = LayoutInflater.from(context).inflate(R.layout.aboutseparator, null);		
				TextView titleTextView = (TextView)aboutView.findViewById(R.id.title);
				titleTextView.setText(item.getTitle());
				return aboutView;
			}
			
			View aboutView = LayoutInflater.from(context).inflate(R.layout.aboutitem, null);
			
			TextView titleTextView = (TextView)aboutView.findViewById(R.id.title);
			titleTextView.setText(item.getTitle());
			TextView detailsTextView = (TextView)aboutView.findViewById(R.id.details);
			detailsTextView.setText(item.getDescription());
			if (item.getIcon() > 0) {
				ImageView iconImageView = (ImageView)aboutView.findViewById(R.id.icon);
				iconImageView.setImageResource(item.getIcon());
			}
			
			return aboutView;
		}
		
		@Override
		public boolean areAllItemsEnabled() {
			return false;
		}
		
		@Override
		public boolean isEnabled(int position) {
			return !items.get(position).isSeparator() && items.get(position).getAction() != null;
		}
		
		
		
		public class AboutItem {
			private String title;
			private String description;
			private String action;
			private int icon;
			private boolean separator;
			
			public AboutItem(String title, String description, String action, int icon, boolean separator) {
				this.title = title;
				this.description = description;
				this.action = action;
				this.icon = icon;
				this.separator = separator;
			}
			
			public String getTitle() {
				return title;
			}
			
			public String getDescription() {
				return description;
			}
			
			public String getAction() {
				return action;
			}
			
			public int getIcon() {
				return icon;
			}
			
			public boolean isSeparator() {
				return separator;
			}
		}
	}
}